//
//  MeViewController.m
//  FWeChat
//
//  Created by iSolar on 2017/10/24.
//  Copyright © 2017年 NOOrganization. All rights reserved.
//

#import "MeViewController.h"
#import "FWeChat-Swift.h"
#import "MeSctionOneController.h"

@interface MeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

/**  图片数组  **/
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - kTabBarH - kViewFrameY) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // tableView 偏移20/64适配
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _tableView.separatorColor = [UIColor colorWithHexString:@"e2e2e2"];
    _tableView.backgroundColor = [UIColor colorWithHexString:@"eeecf4"];
    [_tableView registerNib:[UINib nibWithNibName:@"MeTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"MeSectionOneCell" bundle:nil] forCellReuseIdentifier:@"cell0"];
    
    _dataArray = @[@[@{@"title":@"小宇宙", @"img":@"robot", @"desc":@"Be Better!",@"erImg":@"er",@"wechatNum":@"走不走",@"more":@"更多"}],@[@{@"title":@"钱包", @"img":@"MoreMyBankCard"}],@[@{@"title":@"收藏", @"img":@"MoreMyFavorites"},@{@"title":@"相册", @"img":@"MoreMyAlbum"},@{@"title":@"卡包", @"img":@"MyCardPackageIcon"},@{@"title":@"表情", @"img":@"emoji"}],@[@{@"title":@"设置", @"img":@"MoreSetting"}]];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionArray = _dataArray[section];
    
    return sectionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *sectionArray = _dataArray[indexPath.section];
    
    if (indexPath.section == 0) {
        MeSectionOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell0"];
        if (!cell) {
            cell = [[MeSectionOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell0"];
        }
        cell.iconView.image = [UIImage imageNamed:sectionArray[indexPath.row][@"img"]];
        cell.titleLabel.text = sectionArray[indexPath.row][@"title"];
        cell.descLabel.text = [NSString stringWithFormat:@"微信号：%@",sectionArray[indexPath.row][@"wechatNum"]];
        cell.scanImgV.image = [UIImage imageNamed:sectionArray[indexPath.row][@"erImg"]];
        
        return cell;
    }
    
    static NSString *cellIndentifier = @"cell";
    MeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

    if (cell == nil) {
        cell = [[MeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }

    cell.iconView.image = [UIImage imageNamed:sectionArray[indexPath.row][@"img"]];
    cell.titleLabel.text = sectionArray[indexPath.row][@"title"];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 100;
    }
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *sectionArray = _dataArray[indexPath.section];
    if (indexPath.section == 0) {
        MeSctionOneController *vc = [[MeSctionOneController alloc] init];
        vc.dataArray = sectionArray;
        vc.title = @"个人信息";
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else if(indexPath.section == 1) {
        
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            
        } else if (indexPath.row == 1) {
            
        } else if (indexPath.row == 2) {
            
        } else if (indexPath.row == 3) {
            
        }else {
            
        }
    } else {
        
    }
    
}

// tableView 如果是Gruop类型的话，section之间的间距变宽，执行返回高度的同时还需要执行return UIView的代理
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

@end
